package com.book.servlet;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.book.dao.DownloadFileDao;
import com.book.domain.DownloadFile;

/**
 * Servlet implementation class UploadFileServlet
 */
public class UploadFileServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UploadFileServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	@SuppressWarnings("deprecation")
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		long sizeMax = 4 * 1024 * 1024;// 最大上传文件大小为4M
		DiskFileItemFactory diskFileItemFactory=new DiskFileItemFactory();
		diskFileItemFactory.setSizeThreshold(4096);
		diskFileItemFactory.setRepository(new File(request.getRealPath("/")+"download/"));// 设置上传文件目录
	    ServletFileUpload servletFileUpload=	new ServletFileUpload(diskFileItemFactory);
	    servletFileUpload.setSizeMax(sizeMax);//設置最大上傳文件大小
	    
	    String path;//上传文件的本地全路径名称
	    String qulifyfilename;//上传文件的原始全文件名
	    String filerealname = null;
	    String extname = null;//上传文件的扩展名
	    String newfilename;
	    String prefix = null;
	    
	    try {
		List fileitemList=	servletFileUpload.parseRequest(request);
		for (Object fileObject:fileitemList) {
		FileItem fileItem=	(FileItem) fileObject;
		if (fileItem.isFormField()) {
			request.setAttribute(fileItem.getFieldName(), fileItem.getString("utf-8"));
		}else {
		   path=fileItem.getName();
		   qulifyfilename=  path.substring(path.lastIndexOf("\\")+1);
		   filerealname=qulifyfilename.substring(0, qulifyfilename.lastIndexOf(".")-1);
		   
		   extname= qulifyfilename.substring(qulifyfilename.lastIndexOf(".")+1);
		  long now= System.currentTimeMillis();
		  prefix=String.valueOf(now);
		  newfilename=request.getRealPath("/")+"download/"+prefix+"."+extname;
		  fileItem.write(new File(newfilename));
		  System.out.println(newfilename);
			System.out.println("文件写入成功");
		}
		
		}
		} catch (FileUploadException e) {
			
			e.printStackTrace();
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		DownloadFile downloadFile=new DownloadFile();
		downloadFile.setFilememory((String)request.getAttribute("filememory"));
		downloadFile.setFilerealname(filerealname);
		downloadFile.setFilename(prefix+"."+extname);
		DownloadFileDao downloadFileDao=new DownloadFileDao();
		downloadFileDao.addfile(downloadFile);
		System.out.println("文件上传插入数据库成功");
	    
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	  doGet(request, response);
	}

}
